﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Dycox.Reporting
{
    public class Line : ReportElement
    {
        public Line()
        {
        }

        public Line(PointF start, PointF end)
        {
            StartPoint = start;
            EndPoint = end;
        }


        public PointF StartPoint
        {
            get { return new PointF(_x1, _y1); }
            set
            {
                _x1 = value.X;
                _y1 = value.Y;
            }
        }

        public PointF EndPoint
        {
            get { return new PointF(_x2, _y2); }
            set
            {
                _x2 = value.X;
                _y2 = value.Y;
            }
        }

        private float _x1;
        [XmlAttribute("x1")]
        public float X1
        {
            get { return _x1; }
            set { _x1 = value; }
        }

        private float _y1;
        [XmlAttribute("y1")]
        public float Y1
        {
            get { return _y1; }
            set { _y1 = value; }
        }

        private float _x2;
        [XmlAttribute("x2")]
        public float X2
        {
            get { return _x2; }
            set { _x2 = value; }
        }

        private float _y2;
        [XmlAttribute("y2")]
        public float Y2
        {
            get { return _y2; }
            set { _y2 = value; }
        }

        protected internal override RectangleF GetBounds(RenderingContext context)
        {
            return new RectangleF(
                Math.Min(_x1, _x2),
                Math.Min(_y1, _y2),
                Math.Abs(_x2 - _x1),
                Math.Abs(_y2 - _y1));
        }


        protected internal override void Render(RenderingContext context)
        {
            LineStyle style = GetRuntimeStyle().Border;

            if (style.LineWidth > 0f)
            {
                using (Pen pen = new Pen(style.LineColor, style.LineWidth))
                {
                    context.Graphics.DrawLine(pen, StartPoint, EndPoint);
                }
            }

        }
    }
}
